﻿using IRepositoryBll;
using Microsoft.AspNetCore.Mvc;
using RepositoryEntity;
using RepositorySystem.Models;
using RepositorySystem.Models.ConsumableInfoViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RepositorySystem.Controllers
{
    public class ConsumableInfoController : Controller
    {

        private IConsumableInfoBll _consumableInfoBll;
        private IConsumableRecordBll _consumableRecordBll;
        private ICategoryBll _categoryBll;
        public ConsumableInfoController(IConsumableInfoBll consumableInfoBll , ICategoryBll categoryBll , IConsumableRecordBll consumableRecordBll)
        {
            _consumableInfoBll = consumableInfoBll;
            _categoryBll = categoryBll;
            _consumableRecordBll = consumableRecordBll;
        }
        /// <summary>
        /// 耗材信息页面
        /// </summary>
        /// <returns></returns>
        public IActionResult ConsumableInfoListView()
        {
            return View();
        }

        /// <summary>
        /// 增加耗材信息页面
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IActionResult AddConsumableInfoView(AddConsumableInfoModels model)
        {
            return View();
        }
         


        /// <summary>
        /// 添加耗材信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddConsumableInfo(AddConsumableInfoModels model)
        {
            CustomActionResult res = new CustomActionResult();
            if (model.Description == null)
            {
                res.Msg = "耗材描述不能为空";
                return Json(res);
            }
          
            ConsumableInfo consumableInfo = new ConsumableInfo()
            {
                ID = Guid.NewGuid().ToString(),
                Description = model.Description,
                CategoryId = model.CategoryId,
                Name = model.Name,
                Specification = model.Specification,
                Num = model.Num,
                Unit = model.Unit,
                Money = model.Money,
                WarningNum = model.WarningNum,
                CreateTime = DateTime.Now,
                IsDelete = false
            };
            bool isSuccess = _consumableInfoBll.AddConsumableInfo(consumableInfo);
            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Msg = "成功";
                return Json(res);
            }
            else
            {
                return Json(res);
            }

            
        }

        //判断耗材名字是否相同
        public IActionResult IsHasConsumbleInfo(string Name)
        {
            CustomActionResult res = new CustomActionResult();
            bool isHas = _consumableInfoBll.IsHasConsumbleInfo( Name);
            if (isHas)
            {
                res.Msg = "耗材名字已存在";
                return Json(res);
            }
            else
            {
                return Json(res);
            }
        }
        /// <summary>
        /// 获取集合、分页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public IActionResult GetConsumableInfoList(int page, int limit, string description)
        {
            int count;
            object consumableInfo = _consumableInfoBll.GetConsumableInfoListByPage(page, limit, out count, description);

            return Json(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = consumableInfo
            });
        }

        /// <summary>
        /// 编辑耗材信息页面
        /// </summary>
        /// <param name="consumableInfoId"></param>
        /// <returns></returns>
        public IActionResult UpdateConsumableInfoView(string consumableInfoId)
        {
            return View();
        }


        /// <summary>
        /// 更新修改耗材信息
        /// </summary>
        /// <param name="consumableInfoId"></param>
        /// <returns></returns>
       
        public IActionResult GetUpdateConsumableInfo(string consumableInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            ConsumableInfo consumableInfo = _consumableInfoBll.GetConsumableInfo(consumableInfoId);
            //获取物资（耗材）类型数据
            object categoryOptions = _categoryBll.GetCategoryOptions();
            res.IsSuccess = true;
            res.Msg = "成功";
            res.Status = 1;
            res.Data = new
            {
                ConsumableInfo = consumableInfo,
                CategoryOptions = categoryOptions
            };

            return Json(res);
        }

        /// <summary>
        /// 更新耗材信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateConsumableInfo(UpdateConsumableInfoModels model)
        {
            CustomActionResult res = new CustomActionResult();
            if (_consumableInfoBll.UpdateConsumableInfo(model.ID, model.Description, model.CategoryId, model.Name, model.Specification, model.Num, model.Unit, model.Money, model.WarningNum))
            {
                res.IsSuccess = true;
                res.Status = 1;
                res.Msg = "成功";

            }
            return Json(res);
        }


        /// <summary>
        /// 删除耗材管理信息
        /// </summary>
        /// <param name="consumableInfoId"></param>
        /// <returns></returns>
        public IActionResult DeleteConsumableInfo(string consumableInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            string msg = "";
            if (_consumableInfoBll.SoftConsumableInfo(consumableInfoId , out msg))
            {
                res.Status = 1;
                res.Msg = msg;
                res.IsSuccess = true;
                return Json(res);
            }
            else
            {
                res.Msg = msg;
                return Json(res);
            }
        }

        /// <summary>
        /// 获取类型名称下拉选
        /// </summary>
        /// <returns></returns>
        public IActionResult GetSelectOption()
        {
            CustomActionResult res = new CustomActionResult();
            //获取物资（耗材）类型数据
            object categoryOptions = _categoryBll.GetCategoryOptions();
            res.IsSuccess = true;
            res.Msg = "成功";
            res.Status = 1;
            res.Data = new
            {
                CategoryOptions = categoryOptions,
                
            };
            return Json(res);
        }
    }
}
